package com.gitee.zhuyb.refresh.chart;

import com.gitee.zhuyb.util.WordUtil;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Description: 从指定word模板获取折线图图表,然后动态刷新数据
 * @Date: 2021/12/26 10:36
 * @Copyright (C) ZhuYouBin
 */
public class LineChart {

    private static final String RESULT = "H:\\poi\\lineChart_zhuyb.docx";
    private static final String TEMPLATE = "G:\\zhuyoubin_gitee_space\\code\\work-demo\\poi-word\\src\\main\\resources\\多图word模板.docx";
//    private static final String TEMPLATE = "G:\\zhuyoubin_gitee_space\\code\\work-demo\\poi-word\\src\\main\\resources\\雷达图.docx";

    public static void main(String[] args) throws Exception {
        // 1、读取word模板文件
        InputStream is = new FileInputStream(new File(TEMPLATE));
        // 2、创建word文档对象
        XWPFDocument document = new XWPFDocument(is);

        // 3、获取word中的图表对象
//        List<XWPFChart> charts = document.getCharts();
//        System.out.println(charts.size());
//
//        for (XWPFChart chart : charts) {
//            System.out.println(chart.toString());
//        }

        // 获取所有word图表对象
        List<POIXMLDocumentPart> chartRelation = document.getRelations();
        // 遍历chart图表
        Map<String, POIXMLDocumentPart> chartMap = new HashMap<String, POIXMLDocumentPart>();
        for (POIXMLDocumentPart chartPart : chartRelation) {
            // 判断是否为chart图表对象
            if (chartPart instanceof XWPFChart) {
                // 处理key值,这里采用chartPart对象的toString()的值
                /*
                chartPart.toString()的值格式如下所示:
                Name: /word/charts/chart1.xml - Content Type: application/vnd.openxmlformats-officedocument.drawingml.chart+xml
                Name: /word/charts/chart2.xml - Content Type: application/vnd.openxmlformats-officedocument.drawingml.chart+xml
                Name: /word/charts/chart3.xml - Content Type: application/vnd.openxmlformats-officedocument.drawingml.chart+xml
                多个chart对象,只有 chart1.xml、chart2.xml、chart3.xml 不同,其余内容都是一样的
                所以,这里通过获取 /word/charts/chart1.xml 作为map的key值
                 */
                String chartStr = chartPart.toString();
                System.out.println(chartStr);
                chartStr = chartStr.replaceAll("Name:", "")
                        .replaceAll("- Content Type: application/vnd\\.openxmlformats-officedocument\\.drawingml\\.chart\\+xml", "");
                String key = chartStr.trim();
                // 是图表对象,则保存到map容器
                chartMap.put(key, chartPart);
            }
        }
        System.out.println("图表集合: \n" + chartMap);

        POIXMLDocumentPart poixmlDocumentPart = chartMap.get("/word/charts/chart3.xml");


        // 准备图表数据
        // (1)第一个图表数据
        // X轴分类
        List<String> firstRowTitle = new ArrayList<>();
        firstRowTitle.add(""); // 第一列空着,目的是为了存放Y轴分类
        firstRowTitle.add("粉丝数(个)");

        // Y轴分类
        List<String> firstColTitle = new ArrayList<>();
        firstColTitle.add("yItem01");
        firstColTitle.add("yItem02");

        // 数据集
        List<Map<String, String>> rowDataList = new ArrayList<>();

        // 第一行
        for (int i = 0; i < 12; i++) {
            Map<String, String> row = new HashMap<>();
            String suffix = "";
            if (i > 9) {
                suffix = (i+1)+"";
            } else {
                suffix = "0" + (i+1);
            }
            row.put(firstColTitle.get(0), "2021-" + suffix);
            row.put(firstColTitle.get(1), (int)(Math.random()*100 + 1)+"");
            rowDataList.add(row);
        }

        // 刷新
        WordUtil.replaceLineChart(poixmlDocumentPart, firstRowTitle, firstColTitle, rowDataList);

        // 输出新的word文档
        try {
            File file = new File(RESULT);
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fos = new FileOutputStream(RESULT);
            document.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 关闭流
        document.close();
    }

}
